package com.viphuli.app.tool.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import butterknife.Bind;

import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.adapter.ContactAdapter;
import com.viphuli.app.tool.bean.PageBaseBean;
import com.viphuli.app.tool.bean.page.ContactBean;
import com.viphuli.app.tool.common.MyPageHelper;

/**
 * 手动添加
 * 
 * @author li.li
 * 
 */
public class CircleUserAddManualFragment extends BaseProgressFragment implements
		TextWatcher {

	private int circleId;
	private ArrayList<ContactBean> contactList;
	private List<ContactBean> filterList = new ArrayList<ContactBean>();
	private String searchString;
	private Object searchLock = new Object();
	private SearchListTask curSearchTask = null;
	private boolean inSearchMode = false;

	@Bind(R.id.id_listview)
	protected ListView listview;
	@Bind(R.id.id_input_search_query)
	protected EditText mEtSearch;

	private BaseAdapter adapter;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_circle_user_add_manual;
	}

	@Override
	protected void initParams() {
		// 解析参数
		Bundle args = getArguments();
		circleId = args.getInt("circle_id", 0);
		contactList = (ArrayList<ContactBean>) args
				.getSerializable("friend_list");

	}

	@Override
	protected void initView() {
		mEtSearch.addTextChangedListener(this);
	}

	@Override
	protected void initData() {
		if (contactList != null) {
			adapter = new ContactAdapter(this, contactList, circleId);
			listview.setAdapter(adapter);
		}
	}

	@Override
	public void onRequestResonse(PageBaseBean page) {
		super.onRequestResonse(page);

		ViewUtils.toast("添加成功");
	}

	public static void go(Activity act) {
		MyPageHelper.circleUserAddManual.showMyPage(act);
	}

	public static void go(Activity act, int circleId,
			ArrayList<ContactBean> contactList) {

		Bundle args = new Bundle();
		args.putInt("circle_id", circleId);
		args.putSerializable("friend_list", contactList);
		MyPageHelper.circleUserAddManual.showMyPage(act, args);

	}

	@Override
	public void afterTextChanged(Editable arg0) {
		searchString = mEtSearch.getText().toString().trim().toUpperCase();

		if (curSearchTask != null
				&& curSearchTask.getStatus() != AsyncTask.Status.FINISHED) {
			try {
				curSearchTask.cancel(true);
			} catch (Exception e) {
			}

		}
		curSearchTask = new SearchListTask();
		curSearchTask.execute(searchString);
	}

	/**
	 * 检索的异步任务
	 * 
	 * @author Shower
	 * 
	 */
	private class SearchListTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			filterList.clear();

			String keyword = params[0];

			inSearchMode = (keyword.length() > 0);

			if (inSearchMode) {
				// get all the items matching this
				for (ContactBean contact : contactList) {

					boolean isPinyin = contact.getSpellName().toUpperCase()
							.indexOf(keyword.toUpperCase()) > -1;
					boolean isChinese = contact.getRealName().indexOf(keyword) > -1;

					if (isPinyin || isChinese) {
						filterList.add(contact);
					}

				}

			}
			return null;
		}

		protected void onPostExecute(String result) {

			synchronized (searchLock) {

				if (inSearchMode) {

					adapter = new ContactAdapter(
							CircleUserAddManualFragment.this, filterList,
							circleId);
				} else {
					adapter = new ContactAdapter(
							CircleUserAddManualFragment.this, contactList,
							circleId);
				}
				listview.setAdapter(adapter);
			}

		}
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

	}

}
